/*******************************************************************************
File: balance_check
Project: CPKIN
Purpose: This .do file checks for balance between treatment and control groups on endline covariates,
and on baseline outcomes.
Author: Akshay Dixit
Date Created: Mar 19, 2019
Date modified: July 2, 2020
********************************************************************************/

clear all
set more off

***THIS IS THE INPUT THAT MUST BE CHANGED SPECIFIC TO THE USER***
*gl identity ""
*gl user "" 
		   
********************************************************************************

***Creating Global File Paths***'
gl data "$user/data"
gl dof "$user/do files"
gl analysis "$user/analysis"

********************************************************************************

*** ENDLINE COVARIATES ***

u "$data/endline_adult_chief_child_data.dta", clear

* Stratification variable - Chiefdom
gl chiefdom = "bagruwa_chief barri_chief bumpeh_chief fakunya_chief gallinas_perri_chief kaiyamba_chief kamarjei_chief kori_chief kowa_chief kpaka_chief malin_chief sowa_chief"

* Control variables
local control wealth_index female age attend_school fp_visit chief fp child vil_pop
foreach var of local control {
	tab `var'
}

* Label variables
lab var treatment "Treatment"
lab var wealth_index "Wealth index" 
lab var female "Female"
lab var age "Age"
lab var attend_school "Ever attended school" 
lab var fp_visit "Village visited by UNICEF monitoring team"
lab var vil_pop "Village population"

* Setup - create variables for storing output; add column names
g title = "Balance Check for Treatment Assignment" in 1
forv i = 1/5 {
	local lab: word `i' of "N" "Control" "Treatment" "No Controls" "Chiefdom Fixed Effects"
	local name: word `i' of N Control Treatment NoControls ChiefdomFE
	g `name' = "`lab'" in 3
	replace `name' = "(`i')" in 4
}

* Locals - output row
local j = 5
local k = 6

* Loop through outcomes
foreach v in wealth_index female age attend_school fp_visit vil_pop {
	
	** Add row names
	replace title = "`: var l `v''" in `j'
	
	** Control mean
	qui sum `v' if treatment == 0
	replace Control = trim("`: display %10.2f `r(mean)''") in `j'
	
	** Treatment mean
	qui sum `v' if treatment == 1
	replace Treatment = trim("`: display %10.2f `r(mean)''") in `j'

	** Treatment-Control difference, no controls
	qui reg `v' treatment,  vce(cluster village_id1)
	
	replace N = "`e(N)'" in `j'
	local p = 2 * ttail(e(df_r),abs(_b[treatment] / _se[treatment]))
	local stars
	if `p' < 0.10 local stars = "*" 
	if `p' < 0.05 local stars = "**" 
	if `p' < 0.01 local stars = "***"
	replace NoControls = trim("`: display %10.2f _b[treatment]'") + "`stars'" in `j'
	replace NoControls = "(" + trim("`: display %10.2f _se[treatment]'") + ")" in `k'
	eststo NoControls_`v': qui reg `v' treatment //for joint test

	** Treatment-Control difference, pair FE
	qui reg `v' treatment $chiefdom, vce(cluster village_id1)
	
	local p = 2 * ttail(e(df_r), abs(_b[treatment] / _se[treatment]))
	local stars
	if `p' < 0.10 local stars = "*" 
	if `p' < 0.05 local stars = "**" 
	if `p' < 0.01 local stars = "***"
	replace ChiefdomFE = trim("`: display %10.2f _b[treatment]'") + "`stars'" in `j'
	replace ChiefdomFE = "(" + trim("`: display %10.2f _se[treatment]'") + ")" in `k'
	eststo ChiefdomFE_`v': qui reg `v' treatment $chiefdom //for joint test
	
	** Locals - output row
	local j = `j' + 2
	local k = `j' + 1
}

* Locals - output row
local j = `j' + 1
local k = `j' + 1

* Joint test
foreach type in NoControls ChiefdomFE {

	suest `type'_* , vce(cluster village_id1)
	
	/* 
		need to add "_mean" after estimation results because this is how 
		suest output is formatted.
	*/
		
	foreach x in wealth_index female age attend_school fp_visit vil_pop {
		test _b[`type'_`x'_mean: treatment] = 0, notest accum
		}
	
	test _b[`type'_vil_pop_mean: treatment] = 0, accum
		
	replace `type' = trim("`: display %10.2f `r(chi2)''") in `j' 
	replace `type' = trim("`: display %10.2f `r(p)''") in `k'
}

* Add row names (joint test)
replace title = "Joint test Chi square" in `j'		
replace title = "Joint test P-value" in `k'		

* Save
keep title N Control Treatment NoControls ChiefdomFE 
keep in 1/`k'

export excel "$analysis/balance_check_endline_covariates.xlsx", sheet("treatment") sheetrep

********************************************************************************

*** MAIN OUTCOMES ***

u "$data/baseline_master.dta", clear

drop if type_survey == "focal person"

* Covariates
ren gallinas_Perri_chief gallinas_perri_chief

gl chiefdom = "bagruwa_chief barri_chief bumpeh_chief fakunya_chief gallinas_perri_chief kaiyamba_chief kamarjei_chief kori_chief kowa_chief kpaka_chief malin_chief sowa_chief"

foreach var in $chiefdom {
	replace `var' = "0" if `var' == "."
	destring `var', replace
	tab `var'
}

destring treatment, replace
	
	//Have you ever heard of the position of a child welfare focal person?
tab treatment, sum(know_fp)

	//How often do you speak with the focal person in your village?
g ever_spoken_with_fp = (speak_with_fp > 0)
replace ever_spoken_with_fp = . if speak_with_fp == .	
	
	//Is your focal person aware of his/her responsibilities?
ren fp_role fp_aware

	//Do you feel that the people in your village know who the focal person is?
ren peo_know_fp village_people_know_fp
	
	//Do members of your village report incidents of child violations to your village focal person?
ren rpt_vio_to_fp villagers_report_violations

	//Examining missing values in main outcome variables
local outcomes know_fp ever_spoken_with_fp fp_aware village_people_know_fp villagers_report_violations
foreach var of local outcomes {
	di "`var'"
	count if `var' == . | `var' == .d | `var' == .c
	count if `var' == .
	count if `var' == .d
	count if `var' == .c
}

* Replace missing values by 0 when know_fp == 0
local outcomes ever_spoken_with_fp fp_aware village_people_know_fp villagers_report_violations
foreach var of local outcomes {
	replace `var' = 0 if know_fp == 0
}

* Label variables
lab var know_fp	"Ever heard of the position of a CPFP" 
lab var ever_spoken_with_fp "Ever spoken with CPFP"
lab var fp_aware "CPFP aware of their responsibilities"
lab var village_people_know_fp "Do others in village know CPFP"
lab var villagers_report_violations "Do villagers report to CPFP"

* Balance check
cap erase "$analysis/balance_check_baseline_outcomes.xlsx"
putexcel set "$analysis/balance_check_baseline_outcomes.xlsx", replace

putexcel B1 = ("(1)") C1 = ("(2)") D1 = ("(3)") E1 = ("(4)") F1 = ("(5)") 
putexcel B2 = ("Outcome")
putexcel C2 = ("Treatment")
putexcel D2 = ("Control mean")
putexcel E2 = ("p-value")
putexcel F2 = ("Observations")

local row1 = 3
local row2 = 4
local serial = 1

local outcomes know_fp ever_spoken_with_fp fp_aware village_people_know_fp villagers_report_violations
foreach var of local outcomes {
	
	sleep 500
	di "`var'"
	
	qui reg `var' treatment $chiefdom, vce(cluster village_id1)
	
	qui sum `var' if treatment == 0
	local control_mean = (r(mean))
	local control_mean_round = round(`control_mean', 0.001)
	local varlabel : var label `var'
	
	local coef = _b[treatment]
	local coef_round = round(`coef', 0.001)
	local se = _se[treatment]
	local se_round = round(`se', 0.001)
	local p = 2 * (1-normal(abs(_b[treatment]/_se[treatment])))
	local p_round = round(`p', 0.001)
	
	local stars
		if `p' < 0.10 local stars = "*" 
		if `p' < 0.05 local stars = "**" 
		if `p' < 0.01 local stars = "***"
	
	putexcel A`row1' = ("(" + "`serial'" + ")")
	putexcel B`row1' = ("`varlabel'")
	putexcel C`row1' = ("`coef_round'" + "`stars'")
	putexcel C`row2' = ("(" + "`se_round'" + ")") 
	putexcel D`row1' = ("`control_mean_round'")
	putexcel E`row1' = ("`p'")
	putexcel F`row1' = (e(N))

	local row1 = `row1' + 2
	local row2 = `row2' + 2
	local serial = `serial' + 1
}

********************************************************************************

*** INVOLVEMENT OF CPFP/OTHER ACTORS IN CHILD PROTECTION ACTIVITIES***

* Dropping child survey
drop if type_survey == "child"

* Rename outcome variables to match endline
ren care_fp fp_care
ren preg_fp fp_preg
ren outofsch_fp fp_sch

ren outofsch_* *_sch
ren preg_* *_preg
ren care_* *_care

	//Family
ren family_med fam_med
ren oldchild_med oldchd_med
ren olderchild_study oldchd_study
ren parent_med par_med
ren parents_study par_study
ren parent_preg par_preg
ren parents_care par_care

	//Community
ren busrich_pnsh_hvywork bus_rich_pnsh_hvywork
ren busrich_care bus_rich_care
ren busrich_sev_beat bus_rich_sev_beat
ren childclubs_* chdclubs_*
	
	//Government
ren swoker_med sworker_med
ren swkr_pnsh_hvywork sworker_pnsh 
ren swkr_sev_beat sworker_sev_beat 
ren swkr_care sworker_care	
	
	//Teachers - NOTE: BASELINE DATA ON smc_preg IS NOT AVAILABLE
ren teacher_med tchr_med	
	
	//NGOs
ren childfund_med chdfund_med
ren childdfund_study chdfund_study
ren savechildren_med savechdrn_med 
ren savchdrn_needs savechdrn_needs 
ren save_pnsh_hvywork savechdrn_pnsh_hvywork 
ren save_sev_beat savechdrn_sev_beat 
ren save_care savechdrn_care

* Clean outcome variables
foreach var in fp_pnsh_hvywork fp_sev_beat fp_care {
	di "`var'"
	replace `var' = 0 if know_fp == 0
	tab `var'
	tab treatment, sum(`var')
	
		//Create binary versions of the outcome variables
	count if (`var' == . | `var' == .d | `var' == .c)
	count if `var' == .
	count if `var' == .d
	count if `var' == .c
	g `var'_binary = (`var' == 1 | `var' == 2 | `var' == 3)
	replace `var'_binary = . if (`var' == .)	//.d and .c coded as 0
}

foreach var in fp_med fp_study fp_needs fp_preg fp_sch {
	replace `var' = 0 if know_fp == 0
	tab `var'
	
		//Create binary versions of the outcome variables
	count if (`var' == . | `var' == .d | `var' == .c)
	count if `var' == .
	count if `var' == .d
	count if `var' == .c
	g `var'_binary = (`var' == 1 | `var' == 2 | `var' == 3)
	replace `var'_binary = . if (`var' == .)	//.d and .c coded as 0
	tab `var'
}

		//Family
local family fam_med fam_study fam_needs fam_preg fam_sch fam_pnsh_hvywork fam_sev_beat fam_care ///
oldchd_med oldchd_study oldchd_needs oldchd_preg oldchd_sch oldchd_pnsh_hvywork oldchd_sev_beat oldchd_care ///
par_med par_study par_needs par_preg par_sch par_pnsh_hvywork par_sev_beat par_care 

foreach var of local family {
	replace `var' = 0 if (`var' == 4)	//Changing 4 (This problem hasn't happened in the past six months) to 0 for ease of coding
	replace `var' = 0 if (`var' == .d)	//.d is the same as 4 (This problem hasn't happened)
	replace `var' = 0 if (`var' == .c)	//.c means the respondent is unaware of the existence of this stakeholder
}	
	
egen response_family = rowtotal(fam_med fam_study fam_needs fam_preg fam_sch fam_pnsh_hvywork fam_sev_beat fam_care ///
oldchd_med oldchd_study oldchd_needs oldchd_preg oldchd_sch oldchd_pnsh_hvywork oldchd_sev_beat oldchd_care ///
par_med par_study par_needs par_preg par_sch par_pnsh_hvywork par_sev_beat par_care), missing

replace response_family = 1 if (response_family > 0 & response_family != .)
	
	//Community - Village elite, children's clubs, community members, Child Welfare Committee (CWC), religious groups, women's clubs 
local community bus_rich_med bus_rich_study bus_rich_needs bus_rich_preg bus_rich_sch bus_rich_pnsh_hvywork bus_rich_sev_beat bus_rich_care ///
chdclubs_med chdclubs_study chdclubs_needs chdclubs_preg chdclubs_sch chdclubs_pnsh_hvywork chdclubs_sev_beat chdclubs_care ///
com_med com_study com_needs com_preg com_sch com_pnsh_hvywork com_sev_beat com_care ///
cwc_med cwc_study cwc_needs cwc_preg cwc_sch cwc_pnsh_hvywork cwc_sev_beat cwc_care ///
religious_med religious_study religious_needs religious_preg religious_sch religious_pnsh_hvywork religious_sev_beat religious_care ///
wmensclubs_med wmensclubs_study wmensclubs_needs wmensclubs_preg wmensclubs_sch wmensclubs_pnsh_hvywork wmensclubs_sev_beat wmensclubs_care

foreach var of local community {
	replace `var' = 0 if (`var' == 4) 
	replace `var' = 0 if (`var' == .d) 
	replace `var' = 0 if (`var' == .c)
}

egen response_community = rowtotal(bus_rich_med bus_rich_study bus_rich_needs bus_rich_preg bus_rich_sch bus_rich_pnsh_hvywork bus_rich_sev_beat bus_rich_care ///
chdclubs_med chdclubs_study chdclubs_needs chdclubs_preg chdclubs_sch chdclubs_pnsh_hvywork chdclubs_sev_beat chdclubs_care ///
com_med com_study com_needs com_preg com_sch com_pnsh_hvywork com_sev_beat com_care ///
cwc_med cwc_study cwc_needs cwc_preg cwc_sch cwc_pnsh_hvywork cwc_sev_beat cwc_care ///
religious_med religious_study religious_needs religious_preg religious_sch religious_pnsh_hvywork religious_sev_beat religious_care ///
wmensclubs_med wmensclubs_study wmensclubs_needs wmensclubs_preg wmensclubs_sch wmensclubs_pnsh_hvywork wmensclubs_sev_beat wmensclubs_care), missing

replace response_community = 1 if (response_community > 0 & response_community != .)
	
	//Government - Local councilor, Family Support Unit (FSU), Social Development Offidcers (SDOs), Government Social Workers
local government councilor_med councilor_study councilor_needs councilor_preg councilor_sch councilor_pnsh_hvywork councilor_sev_beat councilor_care ///
fsu_med fsu_study fsu_needs fsu_preg fsu_sch fsu_pnsh_hvywork fsu_sev_beat fsu_care ///
sdo_med sdo_study sdo_needs sdo_preg sdo_sch sdo_pnsh_hvywork sdo_sev_beat sdo_care ///
sworker_med sworker_study sworker_needs sworker_preg sworker_sch sworker_pnsh sworker_sev_beat sworker_care

foreach var of local government {
	replace `var' = 0 if (`var' == 4) 
	replace `var' = 0 if (`var' == .d) 
	replace `var' = 0 if (`var' == .c)	
}

egen response_govt = rowtotal(councilor_med councilor_study councilor_needs councilor_preg councilor_sch councilor_pnsh_hvywork councilor_sev_beat councilor_care ///
fsu_med fsu_study fsu_needs fsu_preg fsu_sch fsu_pnsh_hvywork fsu_sev_beat fsu_care ///
sdo_med sdo_study sdo_needs sdo_preg sdo_sch sdo_pnsh_hvywork sdo_sev_beat sdo_care ///
sworker_med sworker_study sworker_needs sworker_preg sworker_sch sworker_pnsh sworker_sev_beat sworker_care), missing

replace response_govt = 1 if (response_govt > 0 & response_govt != .)

	//Chiefs - Paramount chiefs, village chiefs, Section chiefs
local chiefs pc_med pc_study pc_needs pc_preg pc_sch pc_pnsh_hvywork pc_sev_beat pc_care ///
secchief_med secchief_study secchief_needs secchief_preg secchief_sch secchief_pnsh_hvywork secchief_sev_beat secchief_care ///
vilchief_med vilchief_study vilchief_needs vilchief_preg vilchief_sch vilchief_pnsh_hvywork vilchief_sev_beat vilchief_care

foreach var of local chiefs {
	replace `var' = 0 if (`var' == 4)
	replace `var' = 0 if (`var' == .d)
	replace `var' = 0 if (`var' == .c)
}

egen response_chiefs = rowtotal(pc_med pc_study pc_needs pc_preg pc_sch pc_pnsh_hvywork pc_sev_beat pc_care ///
secchief_med secchief_study secchief_needs secchief_preg secchief_sch secchief_pnsh_hvywork secchief_sev_beat secchief_care ///
vilchief_med vilchief_study vilchief_needs vilchief_preg vilchief_sch vilchief_pnsh_hvywork vilchief_sev_beat vilchief_care), missing

replace response_chiefs = 1 if (response_chiefs > 0 & response_chiefs != .)
	
	//Teachers and SMC members
local edu smc_med smc_study smc_needs smc_sch smc_pnsh_hvywork smc_sev_beat smc_care ///
tchr_med tchr_study tchr_needs tchr_preg tchr_sch tchr_pnsh_hvywork tchr_sev_beat tchr_care

foreach var of local edu {
	replace `var' = 0 if (`var' == 4)
	replace `var' = 0 if (`var' == .d)
	replace `var' = 0 if (`var' == .c)
}

egen response_edu = rowtotal(smc_med smc_study smc_needs smc_sch smc_pnsh_hvywork smc_sev_beat smc_care ///
tchr_med tchr_study tchr_needs tchr_preg tchr_sch tchr_pnsh_hvywork tchr_sev_beat tchr_care), missing

replace response_edu = 1 if (response_edu > 0 & response_edu != .)
	
	//Community Health Officers
local cho cho_med cho_study cho_needs cho_preg cho_sch cho_pnsh_hvywork cho_sev_beat cho_care

foreach var of local cho {
	replace `var' = 0 if (`var' == 4)
	replace `var' = 0 if (`var' == .d)
	replace `var' = 0 if (`var' == .c)
}	

egen response_cho = rowtotal(cho_med cho_study cho_needs cho_preg cho_sch cho_pnsh_hvywork cho_sev_beat cho_care), missing

replace response_cho = 1 if (response_cho > 0 & response_cho != .)
		
	//NGOs
local ngos actionaid_med actionaid_study actionaid_needs actionaid_preg actionaid_sch actionaid_pnsh_hvywork actionaid_sev_beat actionaid_care ///
chdfund_med chdfund_study chdfund_needs chdfund_preg chdfund_sch chdfund_pnsh_hvywork chdfund_sev_beat chdfund_care ///
ngos_med ngos_study ngos_needs ngos_preg ngos_sch ngos_pnsh_hvywork ngos_sev_beat ngos_care ///
plan_med plan_study plan_needs plan_preg plan_sch plan_pnsh_hvywork plan_sev_beat plan_care ///
savechdrn_med savechdrn_study savechdrn_needs savechdrn_preg savechdrn_sch savechdrn_pnsh_hvywork savechdrn_sev_beat savechdrn_care ///
unicef_med unicef_study unicef_needs unicef_preg unicef_sch unicef_pnsh_hvywork unicef_sev_beat unicef_care ///
wrldvision_med wrldvision_study wrldvision_needs wrldvision_preg wrldvision_sch wrldvision_pnsh_hvywork wrldvision_sev_beat wrldvision_care

foreach var of local ngos {
	replace `var' = 0 if (`var' == 4)
	replace `var' = 0 if (`var' == .d)
	replace `var' = 0 if (`var' == .c)
}	

egen response_ngos = rowtotal(actionaid_med actionaid_study actionaid_needs actionaid_preg actionaid_sch actionaid_pnsh_hvywork actionaid_sev_beat actionaid_care ///
chdfund_med chdfund_study chdfund_needs chdfund_preg chdfund_sch chdfund_pnsh_hvywork chdfund_sev_beat chdfund_care ///
ngos_med ngos_study ngos_needs ngos_preg ngos_sch ngos_pnsh_hvywork ngos_sev_beat ngos_care ///
plan_med plan_study plan_needs plan_preg plan_sch plan_pnsh_hvywork plan_sev_beat plan_care ///
savechdrn_med savechdrn_study savechdrn_needs savechdrn_preg savechdrn_sch savechdrn_pnsh_hvywork savechdrn_sev_beat savechdrn_care ///
unicef_med unicef_study unicef_needs unicef_preg unicef_sch unicef_pnsh_hvywork unicef_sev_beat unicef_care ///
wrldvision_med wrldvision_study wrldvision_needs wrldvision_preg wrldvision_sch wrldvision_pnsh_hvywork wrldvision_sev_beat wrldvision_care), missing

replace response_ngos = 1 if (response_ngos > 0 & response_ngos != .)

********************************************************************************

* Label variables
lab var fp_pnsh_hvywork_binary "CPFP involvement in punishing adult for forcing child to do heavy work"
lab var fp_sev_beat_binary "Punishing adult for severely beating child"
lab var fp_care_binary "Providing care and protection for child severely beaten"
lab var fp_med_binary "Helping children get medical care/medicine"
lab var fp_study_binary "Providing encouragement to study"
lab var fp_needs_binary "Providing assistance to children for their basic needs"
lab var fp_preg_binary "Helping pregnant minors get medical care"
lab var fp_sch_binary "Helping out of school children return to school"

lab var response_community "Community"
lab var response_govt "Government"
lab var response_chiefs "Chiefs"
lab var response_edu "Teachers and SMC members"
lab var response_cho "Community Health Officers"
lab var response_ngos "NGOs"

* Balance check
putexcel set "$analysis/balance_check_baseline_outcomes.xlsx", modify

local row1 = 13
local row2 = 14
local serial = 6

local outcomes fp_pnsh_hvywork_binary fp_sev_beat_binary fp_care_binary ///
fp_med_binary fp_study_binary fp_needs_binary fp_preg_binary fp_sch_binary ///
response_community response_govt response_chiefs response_edu response_cho response_ngos
foreach var of local outcomes {
	
	sleep 500
	di "`var'"
	
	qui reg `var' treatment $chiefdom, vce(cluster village_id1)
	
	qui sum `var' if treatment == 0
	local control_mean = (r(mean))
	local control_mean_round = round(`control_mean', 0.001)
	local varlabel : var label `var'
	
	local coef = _b[treatment]
	local coef_round = round(`coef', 0.001)
	local se = _se[treatment]
	local se_round = round(`se', 0.001)
	local p = 2 * (1-normal(abs(_b[treatment]/_se[treatment])))
	local p_round = round(`p', 0.001)
	
	local stars
		if `p' < 0.10 local stars = "*" 
		if `p' < 0.05 local stars = "**" 
		if `p' < 0.01 local stars = "***"
	
	putexcel A`row1' = ("(" + "`serial'" + ")")
	putexcel B`row1' = ("`varlabel'")
	putexcel C`row1' = ("`coef_round'" + "`stars'")
	putexcel C`row2' = ("(" + "`se_round'" + ")") 
	putexcel D`row1' = ("`control_mean_round'")
	putexcel E`row1' = ("`p'")
	putexcel F`row1' = (e(N))

	local row1 = `row1' + 2
	local row2 = `row2' + 2
	local serial = `serial' + 1
}

********************************************************************************

*** CHILD PROTECTION ***

u "$data/baseline_master.dta", clear

ren gallinas_Perri_chief gallinas_perri_chief

gl chiefdom = "bagruwa_chief barri_chief bumpeh_chief fakunya_chief gallinas_perri_chief kaiyamba_chief kamarjei_chief kori_chief kowa_chief kpaka_chief malin_chief sowa_chief"

foreach var in $chiefdom {
	replace `var' = "0" if `var' == "."
	destring `var', replace
	tab `var'
}

destring treatment, replace

* Keeping child survey - these outcomes are based entirely on the child survey
keep if type_survey == "child"

* Examine outcome variables
desc hurt_by_chd hurt_by_adult hurt_by_oth_chdrn //Abuse
desc farm_wrk log_frwood heavy_load other_heavy_wrk other_heavywrk1 oth_work_often1 other_heavywrk2 other_heavywrk3 oth_hev_work2 //Heavy work

foreach var in hurt_by_chd hurt_by_adult hurt_by_oth_chdrn farm_wrk log_frwood heavy_load other_heavy_wrk other_heavywrk1 ///
oth_work_often1 other_heavywrk2 other_heavywrk3 oth_hev_work2 {
	di "`var'"
	tab `var'
}

ren hurt_by_chd child_abuse
	
g other_heavy_work = (other_heavy_wrk == "yes" | other_heavywrk2 == "yes")
replace other_heavy_work = . if other_heavy_wrk == ""

foreach var in log_frwood heavy_load {
	replace `var' = "3" if `var' == "Always (4-7 times a week)"
	replace `var' = "2" if `var' == "Ofter (1-3 times a week)"
	replace `var' = "1" if `var' == "Sometimes(1-3 times a month)"
	replace `var' = "0" if `var' == "Never"
	destring `var', replace
}

g any_farm_work = (farm_wrk == 1 | farm_wrk == 2 | farm_wrk == 3)
replace any_farm_work = . if farm_wrk == .

g any_log_firewood = (log_frwood == 1 | log_frwood == 2 | log_frwood == 3)
replace any_log_firewood = . if log_frwood == .

g any_heavy_load = (heavy_load == 1 | heavy_load == 2 | heavy_load == 3)
replace any_heavy_load = . if heavy_load == .

* Label variables

lab var child_abuse "Whether there have been incidents of child abuse in village"
lab var any_farm_work "Whether child did hard work on the farm"
lab var any_log_firewood "Whether logged for firewood"
lab var any_heavy_load "Whether carried heavy loads"
lab var other_heavy_work "Whether child did any other heavy work"

* Balance check

putexcel set "$analysis/balance_check_baseline_outcomes.xlsx", modify

local row1 = 41
local row2 = 42
local serial = 20

local outcomes child_abuse any_farm_work any_log_firewood any_heavy_load other_heavy_work
foreach var of local outcomes {
	
	sleep 500
	di "`var'"
	
	qui reg `var' treatment $chiefdom, vce(cluster village_id1)
	
	qui sum `var' if treatment == 0
	local control_mean = (r(mean))
	local control_mean_round = round(`control_mean', 0.001)
	local varlabel : var label `var'
	
	local coef = _b[treatment]
	local coef_round = round(`coef', 0.001)
	local se = _se[treatment]
	local se_round = round(`se', 0.001)
	local p = 2 * (1-normal(abs(_b[treatment]/_se[treatment])))
	local p_round = round(`p', 0.001)
	
	local stars
		if `p' < 0.10 local stars = "*" 
		if `p' < 0.05 local stars = "**" 
		if `p' < 0.01 local stars = "***"
	
	putexcel A`row1' = ("(" + "`serial'" + ")")
	putexcel B`row1' = ("`varlabel'")
	putexcel C`row1' = ("`coef_round'" + "`stars'")
	putexcel C`row2' = ("(" + "`se_round'" + ")") 
	putexcel D`row1' = ("`control_mean_round'")
	putexcel E`row1' = ("`p'")
	putexcel F`row1' = (e(N))

	local row1 = `row1' + 2
	local row2 = `row2' + 2
	local serial = `serial' + 1
}

********************************************************************************

clear

